<template>
  <el-form ref="form" :label-position="labelPosition" label-width="120px" :model="formDialog" :rules="rules">
    <el-form-item label="姓名" prop="username">
      <el-input v-model="formDialog.username" style="width:50%" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="电话" prop="mobile">
      <el-input v-model="formDialog.mobile" style="width:50%" placeholder="请输入电话" />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker
        v-model="formDialog.timeOfEntry"
        style="width:50%"
        type="week"
        format="yyyy 第 WW 周"
        placeholder="请选择入职时间"
      />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select v-model="formDialog.formOfEmployment" style="width:50%" placeholder="请选择活动区域">
        <el-option
          v-for="item in hireType"
          :key="item.id"
          :label="item.value"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input v-model="formDialog.workNumber" style="width:50%" placeholder="请输入工号" />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <el-input v-model="formDialog.departmentName" style="width:50%" placeholder="请输入部门" @focus="departmentNameFocus" />
      <!-- 树形结构 -->
      <div v-if="showTree">
        <el-tree :data="treeData" :props="defaultProps" @node-click="handleNodeClick" />
      </div>
    </el-form-item>
    <el-form-item label="转正时间">
      <el-date-picker
        v-model="formDialog.correctionTime"
        type="week"
        style="width:50%"
        format="yyyy 第 WW 周"
        placeholder="转正时间"
      />
    </el-form-item>
    <el-form-item>
      <el-button @click="cancelList">取消</el-button>
      <el-button type="primary" @click="onSubmit">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { addEmployees } from '@/api/employees'
import employees from '@/constant/employees'
import { getDepartments } from '@/api/departments'
import { transTree } from '@/utils/index'
export default {
  name: 'EmpDialog',
  data() {
    return {
      hireType: employees.hireType, // 聘用形式转换
      formDialog: {
        username: '', // 姓名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 组织名称
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转成时间
      },
      labelPosition: 'right', // 文字对齐方式
      rules: {
        username: [
          { required: true, message: '请输入名字', trigger: 'blur' },
          { min: 1, max: 4, message: '请输入1-4个文字', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号码格式不对', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      },
      treeData: [], // 树形结构
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showTree: false
    }
  },
  created() {

  },
  methods: {
    //   关闭弹窗 重置表单
    formReset() {
      this.$refs.form.resetFields()
    },
    departmentNameFocus() {
    //  鼠标进入时触发
      this.getDepartments()
    },
    // 获取组织架构
    async getDepartments() {
      const res = await getDepartments()

      // 当数据返回以后，展示树行结构
      this.showTree = true
      this.treeData = transTree(res.data.depts, '')
    },

    handleNodeClick(data) {
    //   console.log(data)
      if (data && data.children.length !== 0) return
      this.formDialog.departmentName = data.name
      //   隐藏tree
      this.showTree = false
    },
    //   确定
    onSubmit() {
      // 兜底校验
      this.$refs.form.validate(async valid => {
        if (!valid) return
        const res = await addEmployees(this.formDialog)
        // 成功提示
        this.$message.success(res.message)
        // 关闭弹窗
        this.$emit('shutDown')
        // console.log(res)
      })
    },
    // 取消
    cancelList() {
      this.$emit('cancel')
    }
  }

}
</script>

<style scoped lang="scss">
form.el-form {
  padding: 0px 60px 0px 0px;
}

.el-input {
  width: 100% !important;
}

input.el-input__inner {
  width: 100% !important;
}

.el-select {
  width: 100% !important;
}

.el-input.el-input--suffix {
  width: 100% !important;
}
</style>
